package com.sinven.service;

import com.sinven.common.ActionResult;
import com.sinven.common.Pager;
import com.sinven.mapper.ArticleMapper;
import com.sinven.mapper.CategoryMapper;
import com.sinven.pojo.Article;
import com.sinven.pojo.Category;
import com.sinven.vo.ArticleCondition;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class ArticleServiceImpl implements ArticleService {

    @Autowired
    private ArticleMapper articleMapper;

    @Autowired
    private CategoryMapper categoryMapper;

    @Override
    public long countArticle(int categoryId) {
        long count = articleMapper.countArticle(categoryId);
        log.debug("该分类下文章数量为{}", count);
        return count;
    }

    @Override
    public ActionResult<List<Article>> queryArticles(String code, Integer page, int pageSize) {
        ActionResult<List<Article>> actionResult = new ActionResult<>();
        //得到分类ID
        Category category = categoryMapper.queryCategoryByCode(code);
        if (category == null) {
            actionResult.setCode(500);
            actionResult.setMsg("无此分类");
            return actionResult;
        }
        //得到分类文章数目
        long count = articleMapper.countArticle(category.getId());
        if (count == 0) {
            actionResult.setCode(500);
            actionResult.setMsg("此分类无文章");
            return actionResult;
        }
        //得到分类下文章
        int startIndex = 0;
        if (page != null || page > 0) {
            startIndex = (page - 1) * pageSize;
        }
        List<Article> articles = articleMapper.queryArticles(category.getId(), startIndex, pageSize);

        actionResult.setCode(0);
        actionResult.setCount(count);
        actionResult.setData(articles);
        return actionResult;
    }

    @Override
    public boolean updateArticle(Article article) {
        int rows = articleMapper.updateArticle(article);
       
        return rows>0;
    }

    @Override
    public boolean addArticle(Article article) {
        int rows = articleMapper.addArticle(article);
        return rows>0;
    }

    @Override
    public boolean deleteArticle(long articleId) {
        int rows = articleMapper.deleteArticle(articleId);
       return rows>0;
    }

    @Override
    public Article queryArticle(long articleId) {
        return articleMapper.queryArticle(articleId);
    }

    @Override
    public List<Article> queryArticleByCondition(ArticleCondition condition) {

        List<Article> articles = articleMapper.queryByCondition(condition);
        return articles;
    }

    @Override
    public Pager<Article> queryArticles(String code, Long page) {
        //得到分类ID
        Category category = categoryMapper.queryCategoryByCode(code);
        //得到分类文章数目
        long count = articleMapper.countArticle(category.getId());
        if (count == 0) {
            return null;
        }
        Pager<Article> pager = new Pager<>(count, page, 12);
        List<Article> articles = articleMapper.queryArticles(category.getId(), pager.getStartIndex(), pager.getPageSize());
        pager.setDatas(articles);
        return pager;
    }

    @Override
    public List<Article> queryIndexActivity() {
        List<Article> articles = articleMapper.queryIndexActivity();
        return articles;
    }
}
